package com.mingtu.jgspatrol.activity;

import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.blankj.utilcode.util.StringUtils;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.hjq.gson.factory.GsonFactory;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.mingtu.common.callback.StringDialogCallback;
import com.mingtu.common.viewpager.LazyViewPager.LazyFragmentPagerAdapter;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.base.MyBaseActivity;
import com.mingtu.jgspatrol.bean.ColumnBean;
import com.mingtu.jgspatrol.databinding.ActivityExchangeBinding;
import com.mingtu.jgspatrol.fragment.ExchangeFragment;
import com.mingtu.jgspatrol.utils.MyConstant;

import java.util.ArrayList;
import java.util.List;

public class ExchangeActivity extends MyBaseActivity<ActivityExchangeBinding> {

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private int type;
    private String[] mTitles;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_exchange;
    }

    @Override
    protected void initView() {
        super.initView();
    }

    @Override
    protected void initData() {
        setModuleTitle("单位新闻");
        MyPagerAdapter mAdapter = new MyPagerAdapter(getSupportFragmentManager());
        binding.viewPager.setAdapter(mAdapter);
    }

    @Override
    protected void getData() {
        getMessageData();
    }

    private class MyPagerAdapter extends LazyFragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        protected Fragment getItem(ViewGroup viewGroup, int position) {
            return mFragments.get(position);
        }
    }

    /**
     * 学习交流数据
     */
    private void getMessageData() {
        OkGo.<String>get(MyConstant.GET_NEWS_TYPE)
                .tag(this)
                .execute(new StringDialogCallback(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {

                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                ColumnBean columnBean = new ColumnBean();
                                columnBean = gson.fromJson(result, ColumnBean.class);
                                List<ColumnBean.ListBean> listBeans = columnBean.getList();

                                if (listBeans != null) {
                                    int size = listBeans.size();
                                    if (size > 0) {
                                        mTitles = new String[size];
                                        for (int i = 0; i < size; i++) {
                                            String typeName = listBeans.get(i).getTypeName();
                                            int id = listBeans.get(i).getId();
                                            mTitles[i] = typeName;
                                            mFragments.add(ExchangeFragment.getInstance(id, typeName));
                                        }


                                        binding.slideTab.setViewPager(binding.viewPager, mTitles, ExchangeActivity.this, mFragments);
                                        binding.slideTab.setOnTabSelectListener(new OnTabSelectListener() {
                                            @Override
                                            public void onTabSelect(int position) {
                                                binding.viewPager.setCurrentItem(position);
                                            }

                                            @Override
                                            public void onTabReselect(int position) {

                                            }
                                        });
//
                                    }
                                }

                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                            }


                        }
                    }
                });
    }
}